home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / unix / mesaamwn.lha / Mesa-Amiwin / demos / glxdemo.c < prev    next >
C/C++ Source or Header  |  1995-12-03  |  2KB  |  123 lines

  1. /* glxdemo.c */
  2.  
  3.  
  4. /*
  5.  * A demonstration of using the GLX functions.  This program is in the
  6.  * public domain.
  7.  *
  8.  * Brian Paul
  9.  */
  10.  
  11.  
  12. #include <GL/gl.h>
  13. #include <GL/glx.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. static void redraw( Display *dpy, Window w )
  18. {
  19.    printf("Redraw event\n");
  20.  
  21.    glClear( GL_COLOR_BUFFER_BIT );
  22.  
  23.    glColor3f( 1.0, 1.0, 0.0 );
  24.    glRectf( -0.8, -0.8, 0.8, 0.8 );
  25.  
  26.    glXSwapBuffers( dpy, w );
  27. }
  28.  
  29.  
  30.  
  31. static void resize( unsigned int width, unsigned int height )
  32. {
  33.    printf("Resize event\n");
  34.    glViewport( 0, 0, width, height );
  35.    glMatrixMode( GL_PROJECTION );
  36.    glLoadIdentity();
  37.    glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  38. }
  39.  
  40.  
  41.  
  42. static Window make_rgb_db_window( Display *dpy,
  43.                   unsigned int width, unsigned int height )
  44. {
  45.    int attrib[] = { GLX_RGBA,
  46.             GLX_RED_SIZE, 1,
  47.             GLX_GREEN_SIZE, 1,
  48.             GLX_BLUE_SIZE, 1,
  49.             GLX_DOUBLEBUFFER,
  50.             None };
  51.    int scrnum;
  52.    XSetWindowAttributes attr;
  53.    unsigned long mask;
  54.    Window root;
  55.    Window win;
  56.    GLXContext ctx;
  57.    XVisualInfo *visinfo;
  58.  
  59.    scrnum = DefaultScreen( dpy );
  60.    root = RootWindow( dpy, scrnum );
  61.  
  62.    visinfo = glXChooseVisual( dpy, scrnum, attrib );
  63.    if (!visinfo) {
  64.       printf("Error: couldn't get an RGB, Double-buffered visual\n");
  65.       exit(1);
  66.    }
  67.  
  68.    /* window attributes */
  69.    attr.background_pixel = 0;
  70.    attr.border_pixel = 0;
  71.    attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  72.    attr.event_mask = StructureNotifyMask | ExposureMask;
  73.    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  74.  
  75.    win = XCreateWindow( dpy, root, 0, 0, width, height,
  76.                 0, visinfo->depth, InputOutput,
  77.                 visinfo->visual, mask, &attr );
  78.  
  79.    ctx = glXCreateContext( dpy, visinfo, NULL, True );
  80.  
  81.    glXMakeCurrent( dpy, win, ctx );
  82.  
  83.    return win;
  84. }
  85.  
  86.  
  87. static void event_loop( Display *dpy )
  88. {
  89.    XEvent event;
  90.  
  91.    while (1) {
  92.       XNextEvent( dpy, &event );
  93.  
  94.       switch (event.type) {
  95.      case Expose:
  96.         redraw( dpy, event.xany.window );
  97.         break;
  98.      case ConfigureNotify:
  99.         resize( event.xconfigure.width, event.xconfigure.height );
  100.         break;
  101.       }
  102.    }
  103. }
  104.  
  105.  
  106.  
  107. main( int argc, char *argv[] )
  108. {
  109.    Display *dpy;
  110.    Window win;
  111.  
  112.    dpy = XOpenDisplay(NULL);
  113.  
  114.    win = make_rgb_db_window( dpy, 300, 300 );
  115.  
  116.    glShadeModel( GL_FLAT );
  117.    glClearColor( 0.5, 0.5, 0.5, 1.0 );
  118.  
  119.    XMapWindow( dpy, win );
  120.  
  121.    event_loop( dpy );
  122. }
  123.